package com.goldplusgold.tradedelay.app.home.live;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.CIntroBean;
import com.goldplusgold.live.bean.VRecordBean;
import com.goldplusgold.live.result.ResultRecords;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.utils.Logger;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.widget.recyclerview.EndlessRecyclerOnScrollListener;
import com.goldplusgold.tradedelay.app.home.live.adapter.ColumnInfoAdapter;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//TODO 代码优化 by hur
public class ColumnInfoActivity extends BaseActivity {
    public static final int TYPE_FROM_MIDDLE = 1 ;
    public static final int TYPE_FROM_MYFOLLOW = 2 ;

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.title_txt)
    TextView titleView;

    @BindView(R.id.column_info_rv)
    RecyclerView mColumnInfoRv ;

    private String columnID;
    int offset = 0;
    int pageSize = 10 ;
    private CIntroBean cIntroBeans ;
    private List<VRecordBean> recordsListBeans ;

    private ColumnInfoAdapter adapter ;

    private int entryType = -1 ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(this.getIntent() != null){
            columnID = this.getIntent().getStringExtra("columnID");
            entryType = this.getIntent().getIntExtra("entryType", -1);
        }
        recordsListBeans = new ArrayList<VRecordBean>();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    //初始化数据
    private void initData() {
        if(TextUtils.isEmpty(columnID)){
            return;
        }
        LVDataManager.getInstance().getIntro( 3 ,columnID, new LiveBaseCallBack<CIntroBean>(this) {
            @Override
            public void onResponse(Call<CIntroBean> call, final Response<CIntroBean> response) {
                //refreshView(response.body());
                super.onResponse(call,response);
                cIntroBeans = response.body() ;
                initAdapter();
                recordsListBeans.clear();
                recordsListBeans.addAll(response.body().getvRecordBeanList());
                offset = offset + response.body().getvRecordBeanList().size() ;
                if(adapter != null){
                    adapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onFailure(Call<CIntroBean> call, Throwable t) {
                super.onFailure(call,t);
                Logger.e("getIntro",t.toString());
            }
        });

    }

    //初始化adapter
    private void initAdapter() {
        if (adapter == null) {
            adapter = new ColumnInfoAdapter(entryType ,this, cIntroBeans, recordsListBeans, columnID, new ColumnInfoAdapter.ColumnInfoAdapterListener() {
                @Override
                public void HistoryItemClick(int realpostion) {
                    //showToastMessage(realpostion+"" , Toast.LENGTH_SHORT);
                    toLiveDetailActivity(realpostion);
                }
            });
            mColumnInfoRv.setLayoutManager(new LinearLayoutManager(this));
            mColumnInfoRv.setAdapter(adapter);

            mColumnInfoRv.addOnScrollListener(new EndlessRecyclerOnScrollListener((LinearLayoutManager) mColumnInfoRv.getLayoutManager()) {
                @Override
                public void onLoadMore(int currentPage) {
                    loadmoreDatas();
                }
            });
        }else{
            adapter.setcIntroBeans(cIntroBeans);
        }
    }

    private void toLiveDetailActivity(int position){
        Intent intent = new Intent(this, LiveDetailActivity.class);
        intent.putExtra("liveID",recordsListBeans.get(position).getLiveID());
        if(recordsListBeans.get(position).getLiveType() == 0){

            intent.putExtra("type", LiveDetailActivity.LiveDetailType.TEXT_LIVE);
        } else{
            intent.putExtra("type", LiveDetailActivity.LiveDetailType.VIDEO_LIVE);
        }
        //intent.putExtra("type" , recordsListBeans.get(position).getLiveType());
        startActivity(intent);
        /*if(entryType == TYPE_FROM_MIDDLE){
            finish();
        }*/
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_column_info;
    }

    @Override
    protected void initView() {
        super.initView();
        iv_back.setVisibility(View.VISIBLE);
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(R.string.column_title);
    }


    @OnClick({R.id.iv_back,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }

    public void loadmoreDatas(){
        LVDataManager.getInstance().getRecords(columnID, offset, pageSize, new Callback<ResultRecords>() {
            @Override
            public void onResponse(Call<ResultRecords> call, Response<ResultRecords> response) {
                ResultRecords beans = response.body();
                if(beans.getvRecordBeanList() != null && beans.getvRecordBeanList().size() != 0){
                    recordsListBeans.addAll(beans.getvRecordBeanList());
                    offset = offset + beans.getvRecordBeanList().size();
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultRecords> call, Throwable t) {
                Logger.e("getRecords",t.toString());
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
